package com.zhongruan;
import java.util.ArrayList;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        ArrayList<user> list = new ArrayList<>();
        loop:
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("----------欢迎来到用户管理系统!----------");
            System.out.println("请选择你要进行的操作：");
            System.out.println("1.增加用户");
            System.out.println("2.删除用户");
            System.out.println("3.修改用户");
            System.out.println("4.查询用户");
            System.out.println("5.退出");
            String index = scanner.next();
            switch (index) {
                case "1":
                    AddUser(list);
                    break;
                case "2":
                    DeleteUser(list);
                    break;
                case "3":
                    UpdateUser(list);
                    break;
                case "4":
                    QueryUser(list);
                    break;
                case "5": {
                    System.out.println("退出成功！");
                    System.exit(0);//停止虚拟机运行
                    //break loop;
                }
                default: {
                    System.out.println("没有这个选项");
                }
            }
        }
    }

    public static void AddUser(ArrayList<user> list) {
        user u = new user();
        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.println("请输入要增加的Id:");
            String id = scanner.next();
            boolean flag = contains(list, id);
            if (flag) {
                System.out.println("id已经存在,请重新录入");
            } else {
                u.setId(id);
                break;
            }
        }
        System.out.println("请输入要增加的姓名:");
        String name = scanner.next();
        u.setName(name);
        System.out.println("请输入要增加的性别:");
        String sex = scanner.next();
        u.setSex(sex);
        System.out.println("请输入要增加的年龄:");
        int age = scanner.nextInt();
        u.setAge(age);
        System.out.println("请输入要增加的电子邮件:");
        String email = scanner.next();
        u.setEmail(email);
        System.out.println("请输入要增加的电话号码:");
        String number = scanner.next();
        u.setPhoneNumber(number);
        list.add(u);
        System.out.println("添加用户成功！");

    }

    public static void DeleteUser(ArrayList<user> list) {
            System.out.println("请输入要删除的Id:");
            Scanner scanner = new Scanner(System.in);
            String id = scanner.next();
            int index = getindex(list, id);//获取索引
            if (index < 0) {
                System.out.println("此Id不存在，删除失败");
            } else {
                list.remove(index);//删除索引对应的用户数据
                System.out.println("id为"+id+"的用户删除成功！");
            }

    }

    public static void UpdateUser(ArrayList<user> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的Id:");
        String id = scanner.next();
        int index = getindex(list, id);//获取索引
        //索引小于0，说明Id不存在
        if (index < 0) {
            System.out.println("此Id不存在，请重新输入");
            return;
        } else {
            //Id存在，获取索引对应的数据进行修改
            user u=list.get(index);
            System.out.println("请输入要修改的姓名:");
            String name = scanner.next();
            u.setName(name);
            System.out.println("请输入要修改的性别:");
            String sex = scanner.next();
            u.setSex(sex);
            System.out.println("请输入要修改的年龄:");
            int age = scanner.nextInt();
            u.setAge(age);
            System.out.println("请输入要修改的电子邮件:");
            String email = scanner.next();
            u.setEmail(email);
            System.out.println("请输入要修改的电话号码:");
            String number = scanner.next();
            u.setPhoneNumber(number);
            System.out.println("用户信息修改成功！");
        }
    }

    public static void QueryUser(ArrayList<user> list) {
        if (list.size() == 0) {
            System.out.println("当前用户列表为空");
            return;
        }
        System.out.println("Id\t\t姓名\t性别\t年龄\t电子邮件\t\t电话号码");
        for (int i = 0; i < list.size(); i++) {
            user u = list.get(i);
            System.out.println(u.getId() + "\t\t" + u.getName() + "\t" + u.getSex()
                    + "\t" + u.getAge() + "\t" + u.getEmail() + "\t\t" + u.getPhoneNumber());
        }
    }
    public static boolean contains(ArrayList<user> list,String id){
        //从list 中找用户id，如果没有则返回true，否则返回false
       /* for (int i = 0; i < list.size(); i++) {
            user u=list.get(i);
            String uid=u.getId();
            if(uid.equals(id)){
                //id已经存在
                return true;
            }
        }
        return false;*/
        return getindex(list,id)>=0;
    }
    //获取要删除学生id的索引
    public static int getindex(ArrayList<user> list,String id){
        for (int i = 0; i < list.size(); i++) {
            user u=list.get(i);
            String uid=u.getId();
            if(uid.equals(id)){
                //id已经存在
                return i;
            }
        }
        return -1;
    }
}
